package com.study.talk.data.repository

import android.util.Log
import com.study.talk.data.local.GroupInfoDao
import com.study.talk.data.model.GroupInfo
import com.study.talk.data.model.GroupUserInfo
import com.study.talk.data.model.vo.GroupUserInfoVo
import com.study.talk.data.remote.GroupApi
import javax.inject.Inject

class GroupRepository @Inject constructor(
    private val groupApi: GroupApi, private val groupInfoDao: GroupInfoDao
) : BaseRepository() {

    companion object {
        private const val TAG = "GroupRepository"
    }

    suspend fun getGroupInfo(
        groupId: Int,
        success: (GroupInfo) -> Unit = {},
        fail: (String) -> Unit = {}
    ): GroupInfo? {
        if (groupInfoDao.has(groupId)) {
            val group = groupInfoDao.query(groupId)
            success(group)
            return group
        } else {
            val response = groupApi.getGroupInfo(groupId)
            if (response.isSuccessful) {
                if (response.body()?.status == 200) {
                    if (response.body()?.data == null) {
                        Log.e(TAG, "getGroupInfo: response.body()?.data is null")
                        fail("response.body()?.data is null")
                        return null
                    } else {
                        groupInfoDao.insert(response.body()?.data)
                        success(response.body()?.data!!)
                        return response.body()?.data
                    }
                } else {
                    Log.e(TAG, "getGroupInfo: fail:msg:${response.body()?.message}")
                    fail(response.body()?.message!!)
                    return null
                }
            } else {
                Log.e(TAG, "request fail, msg:${response.message()}")
                return null
            }
        }
    }

    suspend fun requestGroupInfo(
        groupId: Int,
        success: (GroupInfo) -> Unit = {},
        fail: (String) -> Unit = {}
    ): GroupInfo? {
        val response = groupApi.getGroupInfo(groupId)
        if (response.isSuccessful) {
            if (response.body()?.status == 200) {
                if (response.body()?.data == null) {
                    Log.e(TAG, "requestGroupInfo: response.body()?.data is null")
                    fail("response.body()?.data is null")
                    return null
                } else {
                    if (groupInfoDao.has(groupId)) {
                        groupInfoDao.update(response.body()?.data)
                    } else {
                        groupInfoDao.insert(response.body()?.data)
                    }
                    success(response.body()?.data!!)
                    return response.body()?.data
                }
            } else {
                Log.e(TAG, "requestGroupInfo: fail:msg:${response.body()?.message}")
                fail(response.body()?.message!!)
                return null
            }
        } else {
            Log.e(TAG, "request fail, msg:${response.message()}")
            fail(response.message())
            return null
        }
    }

    suspend fun getGroups(
        success: (List<GroupInfo>) -> Unit = {},
        fail: (String) -> Unit = {}
    ): List<GroupInfo> {
        val response = groupApi.getGroups()
        if (response.isSuccessful) {
            if (response.body()?.status == 200) {
                if (response.body()?.data == null) {
                    Log.e(TAG, "getGroups: response.body()?.data is null")
                    fail("response.body()?.data is null")
                    return listOf()
                } else {
                    success(response.body()?.data!!)
                    return response.body()?.data!!
                }
            } else {
                Log.e(TAG, "getGroups: error,msg:${response.body()?.message}")
                response.body()?.message?.let { fail(it) }
                return listOf()
            }
        } else {
            Log.e(TAG, "getGroups: request fail,msg:${response.message()}")
            fail(response.message())
            return listOf()
        }
    }

    suspend fun searchGroups(
        keyword: String,
        success: (List<GroupInfo>) -> Unit,
        fail: (String) -> Unit
    ) {
        val response = groupApi.searchGroups(keyword)
        handleResponse("searchGroups", response, success, fail)
    }

    suspend fun getMembers(
        groupId: Int, success: (List<GroupUserInfo>) -> Unit, fail: (String) -> Unit
    ) {
        val response = groupApi.getMembers(groupId)
        handleResponse("getMembers", response, success, fail)
    }

    suspend fun exitGroup(groupId: Int, success: (Boolean) -> Unit, fail: (String) -> Unit) {
        val response = groupApi.exitGroup(groupId)
        handleResponse("exitGroup", response, success, fail)
    }

    suspend fun disbandGroup(groupId: Int, success: (Boolean) -> Unit, fail: (String) -> Unit) {
        val response = groupApi.disband(groupId)
        handleResponse("disbandGroup", response, success, fail)
    }

    suspend fun inGroup(
        groupId: Int,
        userId: Int,
        success: (Boolean) -> Unit,
        fail: (String) -> Unit
    ) {
        val response = groupApi.inGroup(
            GroupUserInfoVo(
                groupId,
                userId
            )
        )
        handleResponse("inGroup", response, success, fail)
    }
}